import {Component} from '@angular/core';
import { Platform, ToastController} from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { LoginPage } from '../pages/login/login';
import {JPushService} from "ionic2-jpush";
import {errorHandler} from "@angular/platform-browser/src/browser";
import {LocalStorProvider} from "../providers/local-stor/local-stor";
import {MainPage} from "../pages/main/main";
import {JPush} from "@jiguang-ionic/jpush";
import {MessagePage} from "../pages/message/message";



@Component({
  templateUrl: 'app.html'
})
export class MyApp {

  rootPage:any ;//= MainPage;
  /*backFlag=false;//手机原生返回回退键标识*/

  constructor(private platform: Platform, public statusBar: StatusBar, splashScreen: SplashScreen, public storage:LocalStorProvider,
              private toastCtrl: ToastController, public jpush: JPush) {
    let user_id = this.storage.getKey('user_id').then(user_id=>{
      if(user_id==null)
      {
        this.rootPage=LoginPage;
      }
      else
      {
        this.rootPage=MainPage;
      }
    });

    platform.ready().then(() => {

      this.statusBar.backgroundColorByHexString('#cc0000');
      this.statusBar.styleLightContent();

      splashScreen.hide();

/*      platform.registerBackButtonAction( () => {
        this.existApp();
      })*/

      // home 出app 返回后的事件 清楚通知角标；
      document.addEventListener("resume", () => {
        this.jpush.setApplicationIconBadgeNumber(0);
        console.log("进入，前台展示"); //进入，前台展示
      }, false);
      document.addEventListener("pause", () => {
        this.jpush.setApplicationIconBadgeNumber(0);
        console.log("退出，后台运行"); //退出，后台运行
      }, false);
    });

  }

  setRootPage(){
    let user_id = this.storage.getKey('user_id');
    if(user_id==null)
    {
      this.rootPage=LoginPage;
    }
  }

/*  existApp()
  {
    if(!this.backFlag)
    {
      this.backFlag=true;
      let toast = this.toastCtrl.create({
        message: '再按一次退出程序',
        duration: 3000,
        position: 'bottom'
      });

      toast.onDidDismiss(() => {
        this.backFlag=false;
      });

      toast.present();
    }
    else
    {
      this.platform.exitApp();
    }
  }*/
}
